package com.wutongyu.mannyburger;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class ResetPasswordFragment extends Fragment {

    private static final String TAG = "ResetPasswordFragment";
    private static final String ARG_PHONE = "arg_phone";

    private EditText editTextNewPassword;
    private EditText editTextConfirmPassword;
    private Button buttonResetPassword;
    private UserDatabaseHelper dbHelper;

    private String phone;

    /**
     * 使用工厂方法创建新实例
     */
    public static ResetPasswordFragment newInstance(String phone) {
        ResetPasswordFragment fragment = new ResetPasswordFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PHONE, phone);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            phone = getArguments().getString(ARG_PHONE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_reset_password, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        editTextNewPassword = view.findViewById(R.id.edit_text_new_password);
        editTextConfirmPassword = view.findViewById(R.id.edit_text_confirm_password);
        buttonResetPassword = view.findViewById(R.id.button_reset_password);

        dbHelper = new UserDatabaseHelper(requireContext());

        buttonResetPassword.setOnClickListener(v -> resetPassword());
    }

    private void resetPassword() {
        String newPassword = editTextNewPassword.getText().toString().trim();
        String confirmPassword = editTextConfirmPassword.getText().toString().trim();

        // 基本验证
        if (TextUtils.isEmpty(newPassword)) {
            editTextNewPassword.setError("新密码不能为空");
            editTextNewPassword.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            editTextConfirmPassword.setError("请确认密码");
            editTextConfirmPassword.requestFocus();
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            editTextConfirmPassword.setError("两次输入的密码不一致");
            editTextConfirmPassword.requestFocus();
            return;
        }

        // 更新密码
        boolean isReset = dbHelper.resetPassword(phone, newPassword);

        if (isReset) {
            Toast.makeText(getContext(), "密码重置成功，请使用新密码登录", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "用户 " + phone + " 密码重置成功");
            
            // 导航回登录页面
            navigateToLogin();
        } else {
            Toast.makeText(getContext(), "密码重置失败，请稍后重试", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "用户 " + phone + " 密码重置失败");
        }
    }

    private void navigateToLogin() {
        // 清除回退栈，直接回到登录页面
        FragmentManager fragmentManager = getParentFragmentManager();
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        
        // 重新创建登录页面
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.nav_host_fragment_container, new LoginFragment());
        transaction.commit();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
} 